home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / CopyBits / CopyBitsExamples.c < prev   
Encoding:
Text File  |  1991-04-03  |  8.1 KB  |  314 lines  |  [TEXT/KAHL]

  1.  
  2.  
  3. void
  4. DoColorizedCopyBits()
  5. {
  6. Rect srcRect;
  7. long * bitsPtr;
  8. short iii;
  9. long jjj;
  10. RGBColor    myrgb, savergb;
  11. GDHandle    oldGD;
  12. GWorldPtr    oldGW;
  13. GWorldPtr    myOffGWorld;
  14. PixMapHandle myPixMapHandle;
  15. unsigned short    myRowBytes;
  16. char        mode;
  17.  
  18.     SetRect( &srcRect, 0, 0, 1, 256 );    /*left, top, right, bottom */
  19.     if( !NewGWorld( &myOffGWorld, 32, &srcRect, 0, 0, 0 ) )
  20.     {
  21.         myPixMapHandle = GetGWorldPixMap( myOffGWorld );   /*  7.0 only */
  22. /*---     myPixMapHandle = myOffGWorld->portPixMap;    -----pre-7.0 */
  23.         LockPixels( myPixMapHandle );
  24.         
  25.         
  26. /* get baseAddr good in 32-bit mode */
  27.         bitsPtr = (long *) GetPixBaseAddr( myPixMapHandle );    
  28.         myRowBytes = (**myPixMapHandle).rowBytes & 0x3fff;
  29.  
  30. /* Goto 32-bit addressing mode to access pixels */
  31.         mode = true32b;
  32.         SwapMMUMode( &mode );                        
  33.         for( jjj = 256-1; jjj >= 0; jjj-- )
  34.         {
  35.             *bitsPtr = jjj | (jjj<<8) | (jjj<<16);
  36.             bitsPtr = (long *)((char *)bitsPtr + myRowBytes);
  37.         }
  38. /* Back to old addressing mode. */
  39.         SwapMMUMode( &mode );                                        
  40.         GetForeColor( &savergb );
  41.         myrgb.red = 0xFFFF;
  42.         myrgb.green = 0;
  43.         myrgb.blue = 0;
  44.         RGBForeColor( &myrgb );    
  45.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &srcRect, &thePort->portRect, srcCopy, 0 );
  46.         RGBForeColor( &savergb );
  47.         UnlockPixels( myPixMapHandle );
  48.     }
  49.     DisposeGWorld( myOffGWorld );
  50. }
  51.  
  52. void
  53. CMYColorSeparation()
  54. {
  55. Rect dstRect;
  56. long * bitsPtr;
  57. RGBColor    myrgb, savergb;
  58. GDHandle    oldGD;
  59. GWorldPtr    oldGW;
  60. GWorldPtr    myOffGWorld;
  61. PixMapHandle myPixMapHandle;
  62.  
  63. Rect            bounds;
  64. PicHandle        myPicHandle;
  65.  
  66. #define    PICTResID        1000
  67. #define    ditherCopy        0x40
  68.  
  69.     myPicHandle = GetPicture( PICTResID );
  70.     if( !myPicHandle )
  71.         return;                /* failed -> exit */
  72.     bounds = (*myPicHandle)->picFrame;
  73.     OffsetRect(&bounds, -bounds.left, -bounds.top);    /* To ensure that the scaling below will work for any rect */
  74.     dstRect = bounds;
  75.     dstRect.right *=1.5;                            /* final image = 1.5 times size of src image */
  76.     dstRect.bottom *=1.5;
  77.     OffsetRect( &dstRect, 20, 20 );
  78.     
  79.     bounds.right *=3;                            /* Expand by factor of 3 */
  80.     bounds.bottom *=3;
  81.  
  82.     if( NewGWorld( &myOffGWorld, 32, &bounds, 0, 0, 0 ) == noErr )
  83.     {
  84.         GetGWorld(&oldGW,&oldGD);
  85.         GetForeColor( &savergb );
  86.         SetGWorld(myOffGWorld,nil);
  87.  
  88.         EraseRect( &bounds );        /* clear the GWorld */
  89.         
  90.         myPixMapHandle = GetGWorldPixMap( myOffGWorld );    /* 7.0 only */
  91. /*        myPixMapHandle = myOffGWorld->portPixMap;                 pre-7.0 */
  92.  
  93.         LockPixels( myPixMapHandle );
  94.         DrawPicture( myPicHandle, &bounds );
  95.  
  96.         SetGWorld(oldGW,oldGD);                        /* Copy to window */
  97.  
  98. /* Get the yellow component */
  99.         myrgb.red = 0xFFFF;
  100.         myrgb.green = 0xFFFF;
  101.         myrgb.blue = 0;
  102.         RGBForeColor( &myrgb );    
  103.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  104.         OffsetRect( &dstRect, 220, 0 );
  105.  
  106. /* Get the magenta component */
  107.         myrgb.red = 0xFFFF;
  108.         myrgb.green = 0;
  109.         myrgb.blue = 0xFFFF;
  110.         RGBForeColor( &myrgb );    
  111.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  112.         OffsetRect( &dstRect, -220, 220 );
  113.  
  114. /* Get the cyan component */
  115.         myrgb.red = 0;
  116.         myrgb.green = 0xFFFF;
  117.         myrgb.blue = 0xFFFF;
  118.         RGBForeColor( &myrgb );    
  119.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  120.         OffsetRect( &dstRect, 220, 0 );
  121.  
  122.         myrgb.red = 0;
  123.         myrgb.green = 0;
  124.         myrgb.blue = 0;
  125.         RGBForeColor( &myrgb );    
  126.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  127.             
  128.         RGBForeColor( &savergb );
  129.         UnlockPixels( myPixMapHandle );
  130.         DisposeGWorld( myOffGWorld );
  131.     }
  132.     else
  133.     {
  134.         SysBeep(10);
  135.     }
  136. }
  137.  
  138. void
  139. RGBColorSeparation()
  140. {
  141. Rect dstRect;
  142. long * bitsPtr;
  143. RGBColor    myrgb, savergb;
  144. GDHandle    oldGD;
  145. GWorldPtr    oldGW;
  146. GWorldPtr    myOffGWorld;
  147. PixMapHandle myPixMapHandle;
  148.  
  149. Rect            bounds;
  150. PicHandle        myPicHandle;
  151.  
  152.     myPicHandle = GetPicture( PICTResID );
  153.     if( !myPicHandle )
  154.         return;                /* failed -> exit */
  155.     bounds = (*myPicHandle)->picFrame;
  156.     OffsetRect(&bounds, -bounds.left, -bounds.top);    /* To ensure that the scaling below will work for any rect */
  157.     dstRect = bounds;
  158.     dstRect.right *=1.5;                            /* final image = 1.5 times size of src image */
  159.     dstRect.bottom *=1.5;
  160.     OffsetRect( &dstRect, 20, 20 );
  161.  
  162.     bounds.right *=3;                            /* Expand by factor of 3 */
  163.     bounds.bottom *=3;
  164.  
  165.     if( NewGWorld( &myOffGWorld, 32, &bounds, 0, 0, 0 ) == noErr )
  166.     {
  167.         GetGWorld(&oldGW,&oldGD);
  168.         GetForeColor( &savergb );
  169.         SetGWorld(myOffGWorld,nil);
  170.  
  171.         myrgb.red = 0xffff;
  172.         myrgb.green = 0xffff;
  173.         myrgb.blue = 0xffff;
  174.         RGBForeColor( &myrgb );    
  175.         PaintRect( &bounds );        /* clear the GWorld */
  176.         
  177.          myPixMapHandle = GetGWorldPixMap( myOffGWorld );    /* 7.0 only */
  178. /*        myPixMapHandle = myOffGWorld->portPixMap;                 pre-7.0 */
  179.  
  180.         LockPixels( myPixMapHandle );
  181.         DrawPicture( myPicHandle, &bounds );
  182.         
  183.         SetGWorld(oldGW,oldGD);                        /* Copy to window */
  184.  
  185. /* Get the red component */
  186.         myrgb.red = 0;
  187.         myrgb.green = 0;
  188.         myrgb.blue = 0;
  189.         RGBForeColor( &myrgb );    
  190.         myrgb.red = 0xFFFF;
  191.         myrgb.green = 0;
  192.         myrgb.blue = 0;
  193.         RGBBackColor( &myrgb );    
  194.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  195.         OffsetRect( &dstRect, 220, 0 );
  196.  
  197. /* Get the green component */
  198.         myrgb.red = 0;
  199.         myrgb.green = 0xFFFF;
  200.         myrgb.blue = 0;
  201.         RGBBackColor( &myrgb );    
  202.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  203.         OffsetRect( &dstRect, -220, 220 );
  204.  
  205. /* Get the blue component */
  206.         myrgb.red = 0;
  207.         myrgb.green = 0;
  208.         myrgb.blue = 0xFFFF;
  209.         RGBBackColor( &myrgb );    
  210.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  211.         OffsetRect( &dstRect, 220, 0 );
  212.  
  213.         myrgb.red = 0;
  214.         myrgb.green = 0;
  215.         myrgb.blue = 0;
  216.         RGBForeColor( &myrgb );    
  217.         myrgb.red = 0xffff;
  218.         myrgb.green = 0xffff;
  219.         myrgb.blue = 0xffff;
  220.         RGBBackColor( &myrgb );    
  221.         CopyBits( (BitMap *)*myPixMapHandle, &thePort->portBits, &bounds, &dstRect, ditherCopy+srcCopy, 0 );
  222.             
  223.         RGBForeColor( &savergb );
  224.         UnlockPixels( myPixMapHandle );
  225.         DisposeGWorld( myOffGWorld );
  226.     }
  227.     else
  228.     {
  229.         SysBeep(10);
  230.     }
  231. }
  232.  
  233. Boolean
  234. CreatePixMap( PixMap *myPixMap, short hSize, short vSize, short pixDepth )
  235. {
  236. #define    RGBDirect 16
  237. long    *tmpPtr;
  238. short rowBytes;
  239.  
  240.     rowBytes = pixDepth*hSize/8;
  241.     rowBytes += rowBytes & 0x0001;    /* force rowBytes to be even */
  242.     rowBytes = rowBytes?rowBytes:2;    /* force rowBytes >= 2    */
  243.     
  244.     if( (myPixMap->baseAddr = NewPtr( (long)rowBytes*vSize )) == 0 )    /* Allocate PixMap data */
  245.         return( 0 );                                                /* Abort if noMem */
  246.     myPixMap->rowBytes = rowBytes | 0x8000;
  247.     myPixMap->bounds.top = 0;
  248.     myPixMap->bounds.left = 0;
  249.     myPixMap->bounds.bottom = vSize;
  250.     myPixMap->bounds.right = hSize;
  251.     myPixMap->pmVersion = 0;
  252.     myPixMap->packType = 0;
  253.     myPixMap->packSize = 0;
  254.     myPixMap->hRes = 0x480000;
  255.     myPixMap->vRes = 0x480000;
  256.     myPixMap->pixelType = pixDepth>=16?RGBDirect:0;
  257.     myPixMap->pixelSize = pixDepth;
  258.     myPixMap->cmpCount = pixDepth>=16?3:1;
  259.     myPixMap->cmpSize = pixDepth<16?pixDepth:pixDepth==16?5:8;
  260.     myPixMap->planeBytes = 0;
  261.     if( pixDepth < 16 )
  262.         myPixMap->pmTable = GetCTable( pixDepth );
  263.     else { /* direct: attach a dummy color table to the pixmap */
  264.         myPixMap->pmTable = (CTabHandle)NewHandle( 16 );        /* dummy color table */
  265.         tmpPtr = *(long**)myPixMap->pmTable;
  266.         *tmpPtr++ = (long) pixDepth;                /* seed */
  267.         *tmpPtr++ = 0x80000000;                /* flags and size-1 */
  268.         *tmpPtr++ = 0x0L;                        /* value and red */
  269.         *tmpPtr++ = 0x0L;                        /* green and blue */        
  270.     }
  271.     myPixMap->pmReserved = 0;
  272.     return( 1 );                /* success */
  273. }
  274.  
  275. void
  276. DoStretchedCopyBits()
  277. {
  278. PixMap srcBits;
  279. Rect myRect;
  280. long * bitsPtr;
  281. long jjj;
  282. RGBColor    myrgb;
  283. HSVColor myhsv;
  284.  
  285. #define    pixMapHeight 255
  286.  
  287.     if( !CreatePixMap( &srcBits, 1, pixMapHeight, 32 ) )
  288.         return;
  289.     bitsPtr = (long *) srcBits.baseAddr;
  290.  
  291.     myhsv.saturation = MaxSmallFract;
  292.     myhsv.value = MaxSmallFract;
  293.     for( jjj = pixMapHeight-1; jjj >= 0; jjj-- )
  294.     {
  295.         myhsv.hue = jjj << 8;
  296.         HSV2RGB( &myhsv, &myrgb );
  297.         *bitsPtr++ = (((long)(myrgb.red & 0xFF00) | (myrgb.green >> 8)) << 8) | (myrgb.blue>>8);
  298.     }
  299.         
  300.     SetRect( &myRect, 0, 0, 1, pixMapHeight );    /*left, top, right, bottom */
  301.     
  302.     CopyBits( (BitMap *)&srcBits, &thePort->portBits, &myRect, &thePort->portRect, srcCopy, 0 );
  303.  
  304.     DisposHandle( (Handle) (srcBits.pmTable) );
  305.     DisposPtr( srcBits.baseAddr );
  306. }
  307.  
  308. DoCopyBitsExample()
  309. {
  310.     DoColorizedCopyBits();
  311. }
  312.  
  313.  
  314.